package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Role;
import com.itheima.health.service.CheckGroupService;
import com.itheima.health.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ：
 * @date ：Created in 2019/7/17
 * @description ：
 * @version: 1.0
 */
@RestController
@RequestMapping("/role")
public class RoleController {
    @Reference
    private RoleService roleService;

    @RequestMapping("/add")
    public Result add(@RequestBody Role role, Integer[] permissionIds) {

        try {
            roleService.add(role, permissionIds);
            return new Result(true,"添加角色成功");

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "添加角色失败");
        }
    }

    @RequestMapping("/findPage")
    @ResponseBody
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        try {
            return roleService.pageQuery(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), queryPageBean.getQueryString());

        } catch (Exception e) {
            e.printStackTrace();
            return new PageResult(0L, new ArrayList());
        }
    }

    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            Role byId = roleService.findById(id);
            return new Result(true, MessageConst.ACTION_SUCCESS, byId);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.ACTION_FAIL);
        }
    }

    @RequestMapping("/findpermissionIdsByRoleId")
    public Result findpermissionIdsByRoleId(Integer id) {
        try {
            List<Integer> list = roleService.findpermissionIdsByRoleId(id);
            return new Result(true, MessageConst.ACTION_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConst.ACTION_FAIL);
        }
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody Role role, Integer[] permissionIds) {

        try {
            roleService.edit(role, permissionIds);
            return new Result(true,"更改角色成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"更改角色失败");
        }
    }
    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<Role> all = roleService.findAll();
            return new Result(true, MessageConst.ACTION_SUCCESS,all);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConst.ACTION_FAIL);
        }
    }
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            roleService.deleteById(id);
            return  new Result(true,MessageConst.ACTION_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,e.getMessage());
        }
    }


}
